package com.kfm.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kfm.springboot.domain.SysUser;
import com.kfm.springboot.mapper.SysUserMapper;
import com.kfm.springboot.service.SysUserService;
import com.kfm.springboot.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper mapper;

    @Override
    public SysUser login(SysUser sysUser) {
        if (ObjectUtils.isEmpty(sysUser)){
            // TODO( 业务异常处理 )
        }
        sysUser.setPassword(MD5Util.encode(sysUser.getPassword()));
        // 其实本质上就是一个 根据用户名和密码查询
        List<SysUser> sysUsers = mapper.selectAll(sysUser);
        return sysUsers.size() > 0 ? sysUsers.get(0) : null;
    }

    @Override
    public PageInfo<SysUser> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> sysUsers = mapper.selectAll(null);
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers, 5);
        return pageInfo;
    }
}
